package com.ksyun.seckill.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


import javax.validation.constraints.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * 创建活动的时候，活动的接受vo
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CreateCampaignVO implements Serializable {

    @Size(min = 5, max = 20, message = "name属性约束：5-20个字符")
    private String name ;

    @Size(min = 5, max = 20, message = "descr属性约束：5-20个字符")
    private String descr;

    @NotBlank(message = "imgUrl不能为空")
    private String imgUrl;

    @NotNull(message = "planInventory字段不得为空")
    @Positive(message = "planInventory字段必须大于0")
    @Max(value = 50000, message = "planInventory字段必须小于等于50000")
    private Long planInventory;


    private Long currentInventory;

    @NotNull(message = "beginTime属性不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime beginTime;

    @NotNull(message = "endTime属性不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;

    // 使用@AssertTrue注解指定自定义方法，用于比较startTime和endTime属性
    @AssertTrue(message = "活动开始时间不得晚于结束时间")
    private boolean isValidDate() {
        return beginTime.isBefore(endTime);
    }

    // 使用@AssertTrue注解指定自定义方法，用于比较beginTime和endTime属性
    @AssertTrue(message = "活动结束时间至少大于当前时间五分钟")
    private boolean isValidInterval() {
        long minutesBetween = ChronoUnit.MINUTES.between(beginTime, endTime);
        return minutesBetween > 5;
    }


}
